package servlets;
import dbclass.AutorDAO;
import dbclass.AutorJDBC;
import dbclass.CategoriaDAO;
import dbclass.CategoriaJDBC;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import beans.Autor;
import beans.Categoria;

/**
 * Servlet implementation class Login
 */
public class Login extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Login() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession(true);
		
		AutorDAO autorDAO = new AutorJDBC();
		
		String login = (String)request.getParameter("login"); 
		String senha = (String)request.getParameter("senha"); 
		String url = null;
		
		Autor autor = null;   
		CategoriaDAO catdao = new CategoriaJDBC();
		
		try {  
			autor = autorDAO.login(login,senha);  
			if(autor != null) {
				
				//Seta a sessao de login
				session.putValue("autor_login", autor);
				session.putValue("nome_autor", autor.getNome()); 
				url="Noticias?msg_sucesso=Login efetuado";   
				response.sendRedirect(url); 
			}
			else {
				session.putValue("autor_login", autor);
				url="Noticias?msg_erro=Login ou senha incorreta";   
				response.sendRedirect(url); 
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();  
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block 
			e.printStackTrace(); 
		}

			

	}

}
